package mustry.alone.utils.convert;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import com.alibaba.druid.util.StringUtils;

public class ToString {
	private static final String SYMBOL_AND = "&";
	private static final String SYMBOL_EQUAL = "=";
	private static final String SYMBOL_COMMA = ",";
	
	public static String map(Map<String,Object> map){
		if(map == null || map.size() == 0){
			return "";
		}
		StringBuffer sb = new StringBuffer("");
		for(Map.Entry<String,Object> entry : map.entrySet()){
			sb.append(entry.getKey()).append(SYMBOL_EQUAL).append(entry.getValue()).append(SYMBOL_AND);
		}
		return sb.substring(0,sb.length()-1).toString();
	}
	
	public static String map2signString(Map<String,Object> map){
		if(map == null || map.size() == 0){
			return "";
		}
		List<String> keys = new ArrayList<String>();
		keys.addAll(map.keySet());
		Collections.sort(keys);
		StringBuffer sb = new StringBuffer("");
		for(String key : keys){
			String value = map.get(key).toString();
			if(StringUtils.isEmpty(value) || "sign".equals(value) || "Sign".equals(value) ||
					"sign_type".equals(value) || "SignType".equals(value)){
				continue;
			}
			sb.append(key).append(SYMBOL_EQUAL).append(value).append(SYMBOL_AND);
		}
		String sign = sb.substring(0,sb.length()-1).toString();
		return sign;
	}
	
	public static String listByComma(List<String> list){
		if(list == null || list.size() ==0){
			return "";
		}
		StringBuffer sb = new StringBuffer("");
		for(String str : list){
			sb.append(str.toString()).append(SYMBOL_COMMA);
		}
		return sb.toString().substring(0,sb.length()-1);
	}
}
